package cn.com.note.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatRadioButton;
import android.text.TextPaint;
import android.util.AttributeSet;

import cn.com.note.R;

/**
 * Created by SunFuLin on 2019/1/7.
 */

public class MenuButton extends AppCompatRadioButton {

    float scalingRatio;
    Drawable pressDraw;
    Drawable defaultDraw;
    String bottomText;
    boolean isChecked;
    private int mWidth;
    private int mHeight;
    float bottomTextSize;
    private TextPaint mPaint;
    private Rect mBound;
    private int colorD,colorL,bottomTextColor,bottomTextDefaultColor;
    private float bottomMargin=0f;
    public MenuButton(Context context) {
        super(context,null);
    }
    public MenuButton(Context context, AttributeSet attrs) {
        super(context, attrs,0);
        init(context, attrs);
    }
    private void init(Context context, AttributeSet attrs) {
        //挨着给每个Button加入drawable限制边距以控制显示大小
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.MenuButton);
        scalingRatio=array.getFloat(R.styleable.MenuButton_ScalingRatio,0);
        pressDraw=array.getDrawable(R.styleable.MenuButton_pressDrawable);
        defaultDraw=array.getDrawable(R.styleable.MenuButton_defaultDrawable);

        bottomText=array.getString(R.styleable.MenuButton_bottomText);
        bottomTextSize=array.getDimensionPixelSize(R.styleable.MenuButton_bottomTextSize,0);
        bottomMargin=array.getDimensionPixelOffset(R.styleable.MenuButton_bottomTextMarginBottom,0);
        bottomTextColor=array.getColor(R.styleable.MenuButton_bottomTextColor,Color.BLACK);
        bottomTextDefaultColor=array.getColor(R.styleable.MenuButton_bottomTextDefaultColor,Color.BLACK);
        isChecked=array.getBoolean(R.styleable.MenuButton_isCheckedMenu,false);
        array.recycle();
        setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont.ttf"));
        colorD=ContextCompat.getColor(context, R.color.colorPrimary);
        colorL=ContextCompat.getColor(context, R.color.colorAccent);
        if(defaultDraw!=null&&pressDraw!=null){
            pressDraw.setBounds(0,0, (int) (pressDraw.getMinimumWidth() * scalingRatio),(int)( pressDraw.getMinimumHeight() * scalingRatio));
            defaultDraw.setBounds(0,0, (int) (defaultDraw.getMinimumWidth() * scalingRatio),(int)( defaultDraw.getMinimumHeight() * scalingRatio));
            if(defaultDraw!=null){
                setTextColor(Color.parseColor("#336666"));
                setCompoundDrawables(null, defaultDraw, null, null);
            }
        }
        if(bottomText!=null){
            /**
             * 获得绘制文本的宽和高
             */
            mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setTextSize(bottomTextSize);
            mBound = new Rect();
            mPaint.getTextBounds(bottomText, 0, bottomText.length(), mBound);
        }
        setChecked(isChecked);

    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }
    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
        if(checked){
            if(pressDraw!=null){
                setTextColor(ContextCompat.getColor(getContext(),R.color.colorButton));
                setCompoundDrawables(null, pressDraw, null, null);
            }else {
                setTextColor(colorD);
            }
        }else{
            if(defaultDraw!=null){
                setTextColor(Color.parseColor("#336666"));
                setCompoundDrawables(null, defaultDraw, null, null);
            }else {
                setTextColor(bottomTextDefaultColor);
            }
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if(bottomText!=null){
            mPaint.setColor(bottomTextColor);
            canvas.drawText(bottomText, mWidth / 2 - mBound.width() / 2, getHeight()-bottomMargin, mPaint);
        }
    }
}
